package com.kaoyaya.delivery.adapter;

import java.util.List;

import com.kaoyaya.delivery.activity.R;
import com.kaoyaya.delivery.data.entity.CostRecord;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * 消费记录
 * 
 * @author yubin
 * @data 2013-5-25
 */

public class CostRecordAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<CostRecord> costRecords;

	public CostRecordAdapter(Context context, List<CostRecord>  costRecords) {
		this.costRecords = costRecords;
		inflater = LayoutInflater.from(context);

	}

	@Override
	public int getCount() {
		return costRecords.size();
	}
	
	public void addRecord(List<CostRecord> costRecords) {
		this.costRecords.addAll(costRecords);
	}

	@Override
	public Object getItem(int position) {
		return costRecords.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_con_records, null);
			holder.tvOperateType = (TextView) convertView.findViewById(R.id.tvOperateType);
			holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
			holder.tvRechangeType = (TextView) convertView.findViewById(R.id.tvRechangeType);
			holder.tvAmount = (TextView) convertView.findViewById(R.id.tvAmount);
			holder.tvBalance = (TextView) convertView.findViewById(R.id.tvBalance);
			holder.tvMemo = (TextView) convertView.findViewById(R.id.tvMemo);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		int operateType = costRecords.get(position).getOperateType();
		if (operateType == 1) {
			holder.tvOperateType.setText("充值");
		} else if (operateType == 3) {
			holder.tvOperateType.setText("在线充值");
		} else {
			holder.tvOperateType.setText("消费");
		}
		int rechangeType  = costRecords.get(position).getRechangeType();
		if (rechangeType == 2) {
			holder.tvRechangeType.setText("奖励充值");
		} else if (rechangeType == 3) {
			holder.tvRechangeType.setText("代充值");
		} else {
			holder.tvRechangeType.setText("正常充值");
		}
		holder.tvAmount.setText(String.valueOf(costRecords.get(position).getAmount()));
		holder.tvBalance.setText(String.valueOf(costRecords.get(position).getBalance()));
		holder.tvTime.setText(costRecords.get(position).getTime().toLocaleString());
		holder.tvMemo.setText(costRecords.get(position).getMemo());
		return convertView;
	}

	class ViewHolder {
		TextView tvOperateType,tvTime,tvRechangeType,tvAmount,tvBalance,tvMemo;
	}

}
